package com.jmb.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmb.api.common.exception.LeaseException;
import com.jmb.api.common.result.ResultCodeEnum;
import com.jmb.api.model.entity.ApiTestCacheParameter;
import com.jmb.api.model.entity.ApiTestCase;
import com.jmb.api.model.to.api.cache.ApiTestCacheParameterTO;
import com.jmb.api.service.ApiTestCacheParameterService;
import com.jmb.api.mapper.ApiTestCacheParameterMapper;
import com.jmb.api.service.ApiTestService;
import com.jmb.api.model.vo.api.cache.ApiTestCacheParameterVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author jinbo
* @description 针对表【api_test_cache_parameter(此表是参数缓存，请求参数缓存，响应数据缓存)】的数据库操作Service实现
* @createDate 2025-04-08 13:53:56
*/
@Slf4j
@Service
public class ApiTestCacheParameterServiceImpl extends ServiceImpl<ApiTestCacheParameterMapper, ApiTestCacheParameter>
    implements ApiTestCacheParameterService{
    @Autowired
    private ApiTestCacheParameterMapper apiTestCacheParameterMapper;
    @Autowired
    private ApiTestService apiTestService;

    /**
     * 根据参数名称查询缓存参数
     * @param name 名称
     * @return 缓存参数
     */
    @Override
    public ApiTestCacheParameterVo findCacheParameterByName(String name) {
        ApiTestCacheParameterVo cacheParameterVo = new ApiTestCacheParameterVo();
        if (name ==null){
            cacheParameterVo.setName(name);
            return cacheParameterVo;
        }else {
            LambdaQueryWrapper<ApiTestCacheParameter> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApiTestCacheParameter::getName,name);
            ApiTestCacheParameter cacheParameter = this.getOne(queryWrapper);
            BeanUtils.copyProperties(cacheParameter,cacheParameterVo);
            return cacheParameterVo;
        }

    }

    /**
     * 根据主键查询缓存参数
     * @param processorId 主键
     * @return 缓存参数
     */
    @Override
    public ApiTestCacheParameterVo findCacheParameterById(Integer processorId) {
        if (processorId ==null){
           throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        LambdaQueryWrapper<ApiTestCacheParameter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestCacheParameter::getProcessorId,processorId);
        ApiTestCacheParameter cacheParameter = this.getOne(queryWrapper);
        ApiTestCacheParameterVo cacheParameterVo = new ApiTestCacheParameterVo();
        BeanUtils.copyProperties(cacheParameter,cacheParameterVo);
        return cacheParameterVo;
    }

    /**
     * 查询主键详情
     *
     * @param processorId 主键
     * @param name        名称
     * @return 返回查询的列表信息
     */
    @Override
    public List<ApiTestCacheParameter> checkCacheParameterName(Integer processorId, String name) {
        if (processorId ==null && StringUtils.isBlank(name)){
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        LambdaQueryWrapper<ApiTestCacheParameter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestCacheParameter::getProcessorId,processorId).eq(ApiTestCacheParameter::getName,name);
        return this.list(queryWrapper);
    }

    @Override
    public List<ApiTestCacheParameterVo> findCacheParameterList(ApiTestCacheParameterTO apiTestCacheParameterTO) {
        return apiTestCacheParameterMapper.checkCacheParameterList(apiTestCacheParameterTO);
    }

    /**
     * 保存缓存参数
     * @param apiTestCacheParameter 缓存参数
     */
    @Override
    public void saveCaCheParmeter(ApiTestCacheParameterVo apiTestCacheParameter) {

        if (apiTestCacheParameter.getHaveDefaultValue() == 0){
            if (StringUtils.isBlank(apiTestCacheParameter.getDefaultValue())){
                throw new LeaseException(ResultCodeEnum.WEB_API_CACHE_DEFAULT_VALUE);
            }
        }
        LambdaQueryWrapper<ApiTestCacheParameter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestCacheParameter::getName,apiTestCacheParameter.getName());
        if (this.count(queryWrapper)>0){
            throw  new LeaseException(ResultCodeEnum.WEB_API_CACHE_NAME);
        }
        ApiTestCacheParameter cacheParameter = new ApiTestCacheParameter();
        BeanUtils.copyProperties(apiTestCacheParameter,cacheParameter);
        this.save(cacheParameter);

    }

    /**
     * 修改缓存参数
     * @param apiTestCacheParameter 缓存参数
     */
    @Override
    public void editCaCheParmeter(ApiTestCacheParameterVo apiTestCacheParameter) {
        apiTestService.selectByCaseId(apiTestCacheParameter.getCaseId());
        LambdaQueryWrapper<ApiTestCacheParameter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestCacheParameter::getName,apiTestCacheParameter.getName())
                .ne(ApiTestCacheParameter::getProcessorId,apiTestCacheParameter.getProcessorId());
        if (this.count(queryWrapper)>0){
            throw  new LeaseException(ResultCodeEnum.WEB_API_CACHE_NAME);
        }
        LambdaUpdateWrapper<ApiTestCacheParameter> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApiTestCacheParameter::getProcessorId,apiTestCacheParameter.getProcessorId());
        this.update(apiTestCacheParameter,updateWrapper);
    }

    /**
     * 根据用例编号查询缓存参数主键
     * @param caseId 用例编号
     * @return 主键列表
     */
    @Override
    public List<Integer> selectAllCacheParameterIds(Integer caseId) {
        LambdaQueryWrapper<ApiTestCacheParameter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestCacheParameter::getCaseId,caseId);
        return apiTestCacheParameterMapper.selectList(queryWrapper).stream().map(ApiTestCacheParameter::getProcessorId).toList();
    }

    @Override
    public void removeCacheParameterById(Integer processorId) {
        LambdaQueryWrapper<ApiTestCacheParameter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestCacheParameter::getProcessorId,processorId);
        this.remove(queryWrapper);

    }

    @Override
    public void removeCacheParameterByCaseId(Integer caseId) {
        LambdaQueryWrapper<ApiTestCacheParameter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestCacheParameter::getCaseId,caseId);
        this.remove(queryWrapper);

    }
}




